﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.Services
{
    public class CodeConversionRuleService
    {
        public ServiceResult Create(string userId, Guid dtTableId, Guid dtColumnId, Guid dimId, Guid dimItemId, string rawCodes)
        {
            if(dtTableId == Guid.Empty
                || dtColumnId == Guid.Empty
                || dimId == Guid.Empty
                || dimItemId == Guid.Empty)
            {
                return new ServiceResult(false, ErrorCode.COM_InvalidParameter, ErrorCode.String(ErrorCode.COM_InvalidParameter));
            }

            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.ECodeConversionRule> ccrRep = new EntityRepository<Entities.ECodeConversionRule>(dbContext);

                Entities.ECodeConversionRule eCCR = ccrRep.First(p => p.DtTableId == dtTableId && p.DtColumnId == dtColumnId && p.DimId == dimId && p.DimItemId == dimItemId);
                if(eCCR == null)
                {
                    eCCR = new Entities.ECodeConversionRule();
                    eCCR.Id = Guid.NewGuid();
                    eCCR.DtTableId = dtTableId;
                    eCCR.DtColumnId = dtColumnId;
                    eCCR.DimId = dimId;
                    eCCR.DimItemId = dimItemId;
                    eCCR.CreatedTime = DateTime.Now;
                    eCCR.Creator = userId;
                    eCCR.RawCodes = rawCodes;

                    ccrRep.Create(eCCR);
                    if (dbContext.SaveChanges() > 0)
                    {
                        return new ServiceResult(true, ErrorCode.COM_NoError, string.Empty);
                    }
                    else
                    {
                        return new ServiceResult(false, ErrorCode.COM_DBError, string.Empty);
                    }
                }
                else
                {
                    return new ServiceResult(false, ErrorCode.COM_OBJExist, ErrorCode.String(ErrorCode.COM_OBJExist));
                }
            }
        }

        public ServiceResult Delete(Guid id)
        {
            if(id == Guid.Empty)
            {
                return new ServiceResult(false, ErrorCode.COM_InvalidParameter, ErrorCode.String(ErrorCode.COM_InvalidParameter));
            }

            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.ECodeConversionRule> ccrRep = new EntityRepository<Entities.ECodeConversionRule>(dbContext);

                Entities.ECodeConversionRule eCCR = ccrRep.Get(p => p.Id == id);
                if (eCCR != null)
                {
                    ccrRep.Delete(eCCR);
                    try
                    {
                        if (dbContext.SaveChanges() > 0)
                        {
                            return new ServiceResult(true, ErrorCode.COM_NoError, string.Empty);
                        }
                        else
                        {
                            return new ServiceResult(false, ErrorCode.COM_DBError, string.Empty);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorToTag(Library.LogTAG, string.Format("删除编码转换规则id:{0}出错,ex:{1}", id, ex));
                        return new ServiceResult(false, ErrorCode.COM_Exception, ErrorCode.String(ErrorCode.COM_Exception));
                    }
                }
                else
                {
                    Logger.WarningToTag(Library.LogTAG, string.Format("删除编码转换规则id:{0},但是规则不存在", id));
                    return new ServiceResult(true, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                }
            }
        }

        public ListResult<Views.VCodeConversionRule> GetCodeConversionRules(Guid dtTableId, Guid dtColumnId)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                try
                {
                    EntityRepository<Views.VCodeConversionRule> ccrRep = new EntityRepository<Views.VCodeConversionRule>(dbContext);
                    List<Views.VCodeConversionRule> list = ccrRep.GetList(p => p.DtTableId == dtTableId && p.DtColumnId == dtColumnId);
                    return new ListResult<Views.VCodeConversionRule>(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError), list);
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTAG, string.Format("获取编码转化规则出错,dtTableId:{0},dtColumnId:{1},ex:{2}", dtTableId, dtColumnId, ex));
                    return new ListResult<Views.VCodeConversionRule>(false, ErrorCode.COM_DBError, ErrorCode.String(ErrorCode.COM_DBError));
                }
            }
        }

        public ServiceResult<Views.VCodeConversionRule> GetSingle(Guid id)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                try
                {
                    EntityRepository<Views.VCodeConversionRule> ccrRep = new EntityRepository<Views.VCodeConversionRule>(dbContext);
                    Views.VCodeConversionRule eCCR = ccrRep.Get(p => p.Id == id);
                    if (eCCR ==null)
                    {
                        return new ServiceResult<Views.VCodeConversionRule>(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                    }
                    return new ServiceResult<Views.VCodeConversionRule>(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError), eCCR);
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTAG, string.Format("获取编码转化规则出错,id:{0},ex:{1}", id, ex));
                    return new ServiceResult<Views.VCodeConversionRule>(false, ErrorCode.COM_DBError, ErrorCode.String(ErrorCode.COM_DBError));
                }
            }
        }

        /// <summary>
        /// 更新编码审核规则
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="id"></param>
        /// <param name="rawCodes"></param>
        /// <returns></returns>
        public ServiceResult Update(string userId, Guid id, string rawCodes)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.ECodeConversionRule> ccrRep = new EntityRepository<Entities.ECodeConversionRule>(dbContext);

                Entities.ECodeConversionRule eCCR = ccrRep.Get(p => p.Id == id);
                if (eCCR != null)
                {
                    eCCR.Creator = userId;
                    eCCR.CreatedTime = DateTime.Now;
                    eCCR.RawCodes = rawCodes;
                    ccrRep.Update(eCCR);
                    try
                    {
                        if (dbContext.SaveChanges() > 0)
                        {
                            return new ServiceResult(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError));
                        }
                        else
                        {
                            return new ServiceResult(false, ErrorCode.COM_DBError, ErrorCode.String(ErrorCode.COM_DBError));
                        }
                    }
                    catch(Exception ex)
                    {
                        Logger.ErrorToTag(Library.LogTAG, string.Format("更新编码转换规则id:{0},rawcodes:{1}出错,ex:{2}", id, rawCodes, ex));
                        return new ServiceResult(false, ErrorCode.COM_Exception, ErrorCode.String(ErrorCode.COM_Exception));
                    }
                }
                else
                {
                    return new ServiceResult(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                }
            }
        }
    }
}
